package edu.mju.servicebasic.handler;

import edu.mju.servicebasic.exception.TravelException;
import edu.mju.serviceutils.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理，
 */
@ControllerAdvice
@Slf4j
public class UnifyExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseCode error(Exception e){
        e.printStackTrace();
        return ResponseCode.error().message("全局异常处理");
    }

    @ResponseBody
    @ExceptionHandler(TravelException.class)
    public ResponseCode error(TravelException e){
        e.printStackTrace();
        log.error(e.getMessage());
        return ResponseCode.error().code(e.getCode()).message(e.getMessage());
    }
}
